package com.stargate.soft.scenario;

import java.sql.SQLException;
import com.stargate.bd.ConnectionBD;
import com.stargate.bd.bean.Reservation;
import com.stargate.bd.dao.DAOFactory;
import com.stargate.exception.ReservationDerniereSalleDisponibleException;
import com.stargate.exception.ReservationDurantJoursFeriesException;
import com.stargate.exception.SalleDejaReserveeException;

public class ReservationSalle {

	public ReservationSalle(Reservation reservation) throws SQLException,
			SalleDejaReserveeException,
			ReservationDerniereSalleDisponibleException,
			ReservationDurantJoursFeriesException {
		
		// transaction Serializable
		ConnectionBD.setSerializable();

		// insertion en BD
		try {
			DAOFactory.getReservationDAO().insert(reservation);
		} catch (SQLException e) {
			if (e.getErrorCode() == SalleDejaReserveeException.ERROR_CODE) {
				throw new SalleDejaReserveeException(e.getMessage());
			} else if (e.getErrorCode() == ReservationDerniereSalleDisponibleException.ERROR_CODE) {
				throw new ReservationDerniereSalleDisponibleException(e.getMessage());
			} else if (e.getErrorCode() == ReservationDurantJoursFeriesException.ERROR_CODE) {
				throw new ReservationDurantJoursFeriesException(e.getMessage());
			} else {
				throw e;
			}
		}

		// commit
		ConnectionBD.commit();
	}

}
